<!--
Copyright 2011 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>bfriendr</title>
  <link rel="stylesheet" href="lib/css/go.css">
  <link rel="stylesheet" href="bfriendr.css">
  <script src="lib/js/include-belay.js"></script>
  <script src="/lib/js/common.js"></script>
  <script src="/lib/js/belay-client.js"></script>
  <script src="/lib/js/caps.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script>
</head>
<body id="bfriendr">
<div class="bfriendr bfriendr-welcome">
  <div class="bfriendr-header">
    <h1><span class="bfriendr-logo"
      ><span class="bfriendr-red">b</span>friend<span class="bfriendr-red">r</span></span>
      </h1>
  </div>
  <div class="bfriendr-content">
    <p>Keep in touch with your friends, chat, and exchange capabilities!</p>
    <p>To get going, just fill out this card:</p>
    <div class="bfriendr-mycard">
      <div class="bfriendr-card">
        <ul>
          <li>
            <p class="bfriendr-instructions">This information is visible to your friends and those you've invited.</p>
            <div class="bfriendr-cardform">
              <div class="bfriendr-cardimg"></div>
              <p>Name: <input type="string" name="name" title="Who are you?" /></p>
              <p>E-mail: <input type="string" name="email" title="(optional)"/></p>
              <div class="bfriendr-clear"></div>
            </div>
            <p>Something about yourself:</p>
            <textarea title="Something about yourself"></textarea>
            <button><b>✎</b> Get going with <span class="bfriendr-logo"
              ><span class="bfriendr-red">b</span>friend<span class="bfriendr-red"
              >r</span></button>
            <div class="bfriendr-clear"></div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>
<script>

var myCardImageDiv = $('.bfriendr-cardimg');
var imageFile;

var imageTypeRE = /image\/.*/;
var preventDf = function(e) {
    e.originalEvent.preventDefault();
    return false;
};
myCardImageDiv.bind('dragenter', preventDf);
myCardImageDiv.bind('dragover', preventDf);
myCardImageDiv.bind('drop', function(e) {
  var draggedFile;
  var dt = e.originalEvent.dataTransfer;
  for (var i = 0; i < dt.files.length; ++i) {
    var file = dt.files[i];
    if (!file.type.match(imageTypeRE)) continue;
    draggedFile = file;
    break;
  }
  if (draggedFile) {
    imageFile = draggedFile;

    var imgElt = myCardImageDiv.find('img');
    if (imgElt.length == 0) {
      imgElt = myCardImageDiv.append('<img>').find('img');
    }
    imgElt.attr('src', window.webkitURL.createObjectURL(imageFile));
  }
});

var myCardDiv = $('.bfriendr-mycard');

onBelayReady(function() {
  myCardDiv.find('button').click(function(evt) {
    var fd = new FormData();
    fd.append('name', myCardDiv.find('input[name=name]').val());
    fd.append('email', myCardDiv.find('input[name=email]').val());
    fd.append('notes', myCardDiv.find('textarea').val());
    if (imageFile) {
      fd.append('imageFile', imageFile);
    }
    
    $.ajax({
      url: "/belay/generate",
      cache: false,
      type: 'POST',
      contentType: false,
      processData: false,
      data: fd,
      dataType: 'text',
      success: function(data, status, xhr) {
        data = capServer.dataPostProcess(data);
        belay.outpost.becomeInstance.put(data);
      }
    });
  });
});
</script>
</body>
</html>
